package edu.office.applicationRepository;

import edu.office.entities.Logs;
import edu.office.entities.Student;
import edu.office.entities.TeacherAdmin;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;

import java.time.LocalDateTime;
import java.util.List;

@Named("logsRepositoryA")
@ApplicationScoped
public class LogsRepository extends ApplicationCommonRepository<Logs> {

    @Inject
     TeacherAdminRepository teacherAdminRepository;
    @Inject
     StudentRepository studentRepository;

    public List<Logs> getLogs4Student(Student student) {
        return find("STUDENTID", student).list();
    }

    public List<Logs> getLogs4Teacher(TeacherAdmin teacher) {
        return find("TEACHERID",teacher).list();
    }

    public void logsInTable(Integer teacherid, Integer studentid, String recorderinfo, String tablename, String operationName) {
        if (null == teacherid) {
            teacherid = teacherAdminRepository.getNullTeacherAdmin().getID();
        }
        if (null == studentid) {
            studentid = studentRepository.getNullStudent().getId();
        }
        if (recorderinfo.length() > 20) {
            recorderinfo = recorderinfo.substring(0, 19);
        }
        Logs logs = new Logs();
        logs.setTEACHERID(teacherid);
        logs.setSTUDENTID(studentid);
        logs.setRECORDERINFO(recorderinfo);
        logs.setTABLENAME(tablename);
        logs.setOPERATION(operationName);
        logs.setOPERATIONTIME(LocalDateTime.now());
        persist(logs);
    }
}
